﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TCHSHue.BLL;
using TCHSHue.DAL;

namespace TCHSHue.Modules.IndexControl.TinTuc
{
    public partial class ctrChiTietTinTuc : System.Web.UI.UserControl
    {
        int pSize = 10;
        int pCurrent = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var idBaiViet = Request.QueryString["id"];
                var idTheLoai = Request.QueryString["idTL"];
                var bv = NapBaiViet(idBaiViet);
                if (bv != null)
                    NapDanhSachBaiVietLienQuan(idBaiViet, bv.maTheLoai.Value.ToString());
            }
        }

        private void NapDanhSachBaiVietLienQuan(string idBaiViet, string idTheLoai)
        {
            int temp;
            if (int.TryParse(idTheLoai, out temp))
            {
                var ls = BaiVietBLL.getListTheoTrang(int.Parse(idTheLoai), pSize, pCurrent, (int)TinhTrangBaiViet.DaDuyet);
                if (ls != null && ls.Count >= 1)
                {
                    var item = ls.Where(t => t.MaBaiViet == new Guid(idBaiViet)).SingleOrDefault();
                    if (item != null)
                        ls.Remove(item);
                    ddlDSBaiViet.DataSource = ls;
                    ddlDSBaiViet.DataBind();
                    int maTL = int.Parse(idTheLoai);
                    if ( maTL== Hang.TINTUC)
                        tieuDeDanhSach.InnerText = "Danh sách tin tức";
                    else if(maTL==Hang.THONGBAO)
                        tieuDeDanhSach.InnerText = "Danh sách thông báo";
                    else if (maTL==Hang.THUTUCHANHCHINH)
                        tieuDeDanhSach.InnerText = "Danh sách thủ tục hành chính";
                    if (ls.Count == 0)
                        tieuDeDanhSach.Visible = false;
                }
                else
                    tieuDeDanhSach.Visible = false;
            }
        }

        private BaiViet NapBaiViet(string idBaiViet)
        {
            var bv = BaiVietBLL.getBaiViet(idBaiViet);
            if (bv != null)
            {
                txtChuDe.Text = Server.HtmlDecode(bv.chuDe);
                txtNgayViet.Text = string.Format("{0:dd/MM/yyyy}", bv.ngayViet);
                txtNoiDungTomTat.Text = Server.HtmlDecode(bv.noiDungTomTat);
                txtNoiDung.Text = Server.HtmlDecode(bv.noiDung);
            }
            return bv;
        }

    }
}